#include "CRectangle.h"
using namespace Content;
CRectangle::CRectangle(const float in_a, const float in_b, const float in_c)
{
  a=in_a;
  b=in_b;
  c=in_c;
  vertices = 0;
  indices = 0;
  numVertices = 24;
  numTriangles = 6*2;
  numIndices = numTriangles*3;
  initVertices();
  initIndices();
}
void CRectangle::initVertices()
{
  if (0 ==vertices)
  {
      vertices = new PrimitiveVertex[numVertices]();
  }

  PrimitiveVertex v1 = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v2 = {a, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v3 = {a, 0.0f, b, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v4 = {0.0f, 0.0f, b, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v5 = {0.0f, c, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v6 = {a, c, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v7 = {a, c, b, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};
  PrimitiveVertex v8 = {0.0f, c, b, 0.0f, 0.0f,0.0f, 0.0f, 0.0f};

  float normX,normY,normZ;
  
  for (int face = 0; face < 6; ++face)
  {
    switch (face)
    {
      case 0:
        vertices[face*4]=v1;
	      vertices[face*4+1]=v2;
	      vertices[face*4+2]=v4;
	      vertices[face*4+3]=v3;

        normX = 0.0f;
        normY = -1.0f;
        normZ = 0.0f;
        break;
      case 1:

        vertices[face*4]=v2;
	      vertices[face*4+1]=v6;
	      vertices[face*4+2]=v3;
	      vertices[face*4+3]=v7;

        normX = -1.0f;
        normY = 0.0f;
        normZ = 0.0f;
        break;
      case 2:

        vertices[face*4]=v4;
	      vertices[face*4+1]=v3;
	      vertices[face*4+2]=v8;
	      vertices[face*4+3]=v7;

        normX = 0.0f;
        normY = 0.0f;
        normZ = 1.0f;
        break;
      case 3:

        vertices[face*4]=v1;
	      vertices[face*4+1]=v4;
	      vertices[face*4+2]=v5;
	      vertices[face*4+3]=v8;

        normX = -1.0f;
        normY = 0.0f;
        normZ = 0.0f;
        break;
      case 4:

        vertices[face*4]=v1;
	      vertices[face*4+1]=v5;
	      vertices[face*4+2]=v2;
	      vertices[face*4+3]=v6;

        normX = 0.0f;
        normY = 0.0f;
        normZ = -1.0f;
        break;
      case 5:

        vertices[face*4]=v5;
	      vertices[face*4+1]=v8;
	      vertices[face*4+2]=v6;
	      vertices[face*4+3]=v7;

        normX = 0.0f;
        normY = 1.0f;
        normZ = 0.0f;
        break;
    }


	  vertices[face*4].nX=normX;
	  vertices[face*4].nY=normY;
	  vertices[face*4].nZ=normZ;
	  vertices[face*4].tX=0.0f;
	  vertices[face*4].tY=0.0f;



	  vertices[face*4+1].nX=normX;
	  vertices[face*4+1].nY=normY;
	  vertices[face*4+1].nZ=normZ;
	  vertices[face*4+1].tX=0.0f;
	  vertices[face*4+1].tY=0.0f;

	  vertices[face*4+2].nX=normX;
	  vertices[face*4+2].nY=normY;
	  vertices[face*4+2].nZ=normZ;
	  vertices[face*4+2].tX=0.0f;
	  vertices[face*4+2].tY=0.0f;

	  vertices[face*4+3].nX=normX;
	  vertices[face*4+3].nY=normY;
	  vertices[face*4+3].nZ=normZ;
	  vertices[face*4+3].tX=0.0f;
	  vertices[face*4+3].tY=0.0f;
  }


}

void CRectangle::initIndices()
{
    if (0 == indices)
  {
      indices = new WORD[numIndices]();
  }
  for (int face = 0; face <6; ++face)
  {
    indices[face*6] = face*4 + 0;
    indices[face*6+1] = face*4 + 1;
    indices[face*6+2] = face*4 + 2;

    indices[face*6+3] = face*4 + 2;
    indices[face*6+4] = face*4 + 1;
    indices[face*6+5] = face*4 + 3;
  }
}

void CRectangle::getSize(float& out_a, float& out_b, float& out_c)
{
  out_a = a;
  out_b = b;
  out_c = c;
}
